<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const arr = [1, [2, [3, [4, 5]]], 6]
        // 方法一 flat
        const arr1 = arr.flat(Infinity);
        console.log(arr1);
        //方法二 正则
        const arr2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',');
        console.log(arr2);
        // 方法三 使用reduce
        const flatten = arr => {
            return arr.reduce((pre, cur) => {
                return pre.concat(Array.isArray(cur) ? flatten(cur) : cur);
            }, []);
        }
        const arr3 = flatten(arr);
        console.log(arr3);
        //方法四 函数递归
        const arr4 = [];
        const fn = arr => {
            for (let i = 0; i < arr.length; i++) {
                if (Array.isArray(arr[i])) {
                    fn(arr[i]);
                } else {
                    res4.push(arr[i]);
                }
            }
        }
        fn(arr);
    </script>
</body>

</html>